 #include "pwm.h"
#include "stm32f10x.h"                  // Device header
#define motor0 0
#define motor1 1
#define motor2 2
#define motor3 3
#define motor4 4
#define motor5 5
int motor0_angle=150;
int motor1_angle=90;
int motor2_angle=51;
int motor3_angle=135;
int motor4_angle=150;
int motor5_angle=150;

 void  pwm_init(void)
 {
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 
    TIM_OCInitTypeDef TIM_OCInitStructure; 
    

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); 
	 
    GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE); 

    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); 

   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3| GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_4; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    
    TIM_TimeBaseStructure.TIM_Period = 2000;  
    TIM_TimeBaseStructure.TIM_Prescaler = 720;  
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
		TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
    
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
    TIM_OCInitStructure.TIM_OutputState = ENABLE;  
    TIM_OCInitStructure.TIM_Pulse = 150; 
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
		
    TIM_OC2Init(TIM2, & TIM_OCInitStructure);//1
		TIM_OC3Init(TIM4, & TIM_OCInitStructure);//2
		TIM_OC4Init(TIM4, & TIM_OCInitStructure);//3
		TIM_OC1Init(TIM4, & TIM_OCInitStructure);//4
		TIM_OC2Init(TIM4, & TIM_OCInitStructure);//5
		TIM_OC1Init(TIM3, & TIM_OCInitStructure);//6
		
    TIM_Cmd(TIM2, ENABLE);
		TIM_Cmd(TIM3, ENABLE);
		TIM_Cmd(TIM4, ENABLE);
}
 


 void my_delay(int ms)
 {
	int i = 0;
	while(ms--)
	for(i=0; i<8050; i++);
 }
 void M_set(int motor,int n)
 {
	if(n>50 && n<250)
	{
		if(motor==0){
			TIM_SetCompare2(TIM2, n);
		}else if(motor==1){
			TIM_SetCompare3(TIM4, n);
		}else if(motor==2){
			TIM_SetCompare4(TIM4, n);
		}else if(motor==3){
			TIM_SetCompare1(TIM4, n);
		}else if(motor==4){
			TIM_SetCompare2(TIM4, n);
		}else if(motor==5){
			TIM_SetCompare1(TIM3, n);
		}
		
	}
 }

void M_ctr(char data)
 {
	switch(data)
	{
		//motor0
		case '0':
			if(motor0_angle <= 51)
			{
				motor0_angle=51;
				break;
			}
			motor0_angle-=5;
			M_set(0,motor0_angle);
			break;
			
		case '1':
			if(motor0_angle >= 249)
			{
				motor0_angle=249;
				break;
			}
			motor0_angle+=5;
			M_set(0,motor0_angle);
			break;
			
			//motor1
		case '2':
			if(motor1_angle <= 51)
			{
				motor1_angle=51;
				break;
			}
			motor1_angle-=5;
			M_set(1,motor1_angle);
			break;
			
		case '3':
			if(motor1_angle >= 249)
			{
				motor1_angle=249;
				break;
			}
			motor1_angle+=5;
			M_set(1,motor1_angle);
			break;
			
			//motor2
		case '4':
			if(motor2_angle <= 51)
			{
				motor2_angle=51;
				break;
			}
			motor2_angle-=5;
			M_set(2,motor2_angle);
			break;
			
		case '5':
			if(motor2_angle >= 249)
			{
				motor2_angle=249;
				break;
			}
			motor2_angle+=5;
			M_set(2,motor2_angle);
			break;
			
			//motor3
		case '6':
			if(motor3_angle <= 51)
			{
				motor3_angle=51;
				break;
			}
			motor3_angle-=5;
			M_set(3,motor3_angle);
			break;
			
		case '7':
			if(motor3_angle >= 249)
			{
				motor3_angle=249;
				break;
			}
			motor3_angle+=5;
			M_set(3,motor3_angle);
			break;
			
			//motor4
		case '8':
			if(motor4_angle <= 51)
			{
				motor4_angle=51;
				break;
			}
			motor4_angle-=5;
			M_set(4,motor4_angle);
			break;
			
		case '9':
			if(motor4_angle >= 249)
			{
				motor4_angle=249;
				break;
			}
			motor4_angle+=5;
			M_set(4,motor0_angle);
			break;
			
			//motor5
		case 'a':
			if(motor5_angle <= 51)
			{
				motor5_angle=51;
				break;
			}
			motor5_angle-=5;
			M_set(5,motor5_angle);
			break;
			
		case 'b':
			if(motor5_angle >= 249)
			{
				motor5_angle=249;
				break;
			}
			motor5_angle+=5;
			M_set(5,motor5_angle);
			break;
	}
 }
 
 